import Vue from 'vue';
import Router from 'vue-router';
import Layout from '@/views/Layout';

Vue.use(Router);

export const constantRoutes = [
  {
    path: '/login',
    name: 'Login',
    uri: '/login',
    component: () => import('@/views/Login'),
    meta: {
      hideInMenu: true,
    },
  },
  {
    path: '/404',
    uri: '/404',
    component: () => import('@/views/errorPage/404'),
    meta: { hideInMenu: true },
  },
  {
    path: '/401',
    uri: '/401',
    component: () => import('@/views/errorPage/401'),
    meta: { hideInMenu: true },
  },
];

export const asyncRoutes = [
  {
    path: '/shopUser',
    name: 'shopUser',
    uri: '/shopUser',
    component: Layout,
    meta: { title: '会员管理', icon: 'el-icon-user' },
    children: [
      {
        path: 'wxUser',
        name: 'wxUser',
        uri: '/shopUser/wxUser',
        component: () => import('@/views/shop/wxUser/index'),
        meta: { title: '微信会员', manage: true },
      },
      {
        path: 'collect',
        name: 'collect',
        uri: '/shopUser/collect',
        component: () => import('@/views/shop/collect/index'),
        meta: { title: '会员收藏', manage: true },
      },
      {
        path: 'footprint',
        name: 'footprint',
        uri: '/shopUser/footprint',
        component: () => import('@/views/shop/footprint/index'),
        meta: { title: '会员足迹', manage: true },
      },
      {
        path: 'searchHistory',
        name: 'searchHistory',
        uri: '/shopUser/searchHistory',
        component: () => import('@/views/shop/searchHistory/index'),
        meta: { title: '搜索历史', manage: true },
      },
      {
        path: 'cart',
        name: 'cart',
        uri: '/shopUser/cart',
        component: () => import('@/views/shop/cart/index'),
        meta: { title: '购物车', manage: true },
      },
      {
        path: 'receiptAddress',
        name: 'receiptAddress',
        uri: '/shopUser/receiptAddress',
        component: () => import('@/views/shop/receiptAddress/index'),
        meta: { title: '收货地址', manage: true },
      },
      {
        path: 'comment',
        name: 'comment',
        uri: '/shopUser/comment',
        component: () => import('@/views/shop/comment/index'),
        meta: { title: '用户评论', manage: true },
      },
    ],
  },
  {
    path: '/shopSet',
    name: 'shopSet',
    uri: '/shopSet',
    component: Layout,
    meta: { title: '商城设置', icon: 'el-icon-setting' },
    children: [
      {
        path: 'attributeCategory',
        name: 'attributeCategory',
        uri: '/shopSet/attributeCategory',
        component: () => import('@/views/shop/attributecategory/index'),
        meta: { title: '商品属性种类', manage: true },
      },
      {
        path: 'specification',
        name: 'specification',
        uri: '/shopSet/specification',
        component: () => import('@/views/shop/specification/index'),
        meta: { title: '商品规格', manage: true },
      },
      {
        path: 'category',
        name: 'category',
        uri: '/shopSet/category',
        component: () => import('@/views/shop/category/index'),
        meta: { title: '商品类型', manage: true },
      },
      {
        path: 'channel',
        name: 'channel',
        uri: '/shopSet/channel',
        component: () => import('@/views/shop/channel/index'),
        meta: { title: '首页频道', manage: true },
      },
      {
        path: 'brand',
        name: 'brand',
        uri: '/shopSet/brand',
        component: () => import('@/views/shop/brand/index'),
        meta: { title: '品牌制造商', manage: true },
      },
      {
        path: 'goodsIssue',
        name: 'goodsIssue',
        uri: '/shopSet/goodsIssue',
        component: () => import('@/views/shop/goodsIssue/index'),
        meta: { title: '商品问答', manage: true },
      },
      {
        path: 'feedback',
        name: 'feedback',
        uri: '/shopSet/feedback',
        component: () => import('@/views/shop/feedback/index'),
        meta: { title: '反馈', manage: true },
      },
      {
        path: 'keywords',
        name: 'keywords',
        uri: '/shopSet/keywords',
        component: () => import('@/views/shop/keywords/index'),
        meta: { title: '关键词', manage: true },
      },
      {
        path: 'topic',
        name: 'topic',
        uri: '/shopSet/topic',
        component: () => import('@/views/shop/topic/index'),
        meta: { title: '专题', manage: true },
      },
    ],
  },
  {
    path: '/goodsManage',
    name: 'goodsManage',
    uri: '/goodsManage',
    component: Layout,
    meta: { title: '商品管理', icon: 'el-icon-goods' },
    children: [
      {
        path: 'goods',
        name: 'goods',
        uri: '/goodsManage/goods',
        component: () => import('@/views/shop/goods/index'),
        meta: { title: '所有商品', manage: true },
      },
      {
        path: 'goodsSpecification',
        name: 'goodsSpecification',
        uri: '/goodsManage/goodsSpecification',
        component: () => import('@/views/shop/goodsSpecification/index'),
        meta: { title: '商品规格', manage: true },
      },
      {
        path: 'product',
        name: 'product',
        uri: '/goodsManage/product',
        component: () => import('@/views/shop/product/index'),
        meta: { title: '产品设置', manage: true },
      },
    ],
  },

  {
    path: '/banner',
    name: 'banner',
    uri: '/banner',
    component: Layout,
    meta: { title: '推广管理', icon: 'el-icon-s-operation' },
    children: [
      {
        path: 'adPosition',
        name: 'adPosition',
        uri: '/banner/adPosition',
        component: () => import('@/views/shop/adPosition/index'),
        meta: { title: '广告位置', manage: true },
      },
      {
        path: 'ad',
        name: 'ad',
        uri: '/banner/ad',
        component: () => import('@/views/shop/ad/index'),
        meta: { title: '广告列表', manage: true },
      },
    ],
  },

  {
    path: '/orderManage',
    name: 'orderManage',
    uri: '/orderManage',
    component: Layout,
    meta: { hideInMenu: true, title: '订单管理' },
    children: [
      {
        path: 'order',
        name: 'order',
        uri: '/order',
        component: () => import('@/views/shop/order/index'),
        meta: { title: '所有订单', manage: true },
      },
      {
        path: 'orderRefund',
        name: 'orderRefund',
        uri: '/orderRefund',
        component: () => import('@/views/shop/orderRefund/index'),
        meta: { title: '订单退款', manage: true },
      },
    ],
  },

  {
    path: '/systemManage',
    name: 'systemManage',
    uri: '/systemManage',
    component: Layout,
    meta: { title: '系统管理', icon: 'el-icon-setting' },
    children: [
      {
        path: 'agencyManage',
        name: 'agencyManage',
        uri: '/systemManage/agencyManage',
        component: () => import('@/views/system/organization/index'),
        meta: { title: '机构管理', manage: true },
      },
      {
        path: 'userManage',
        name: 'userManage',
        uri: '/systemManage/userManage',
        component: () => import('@/views/system/user/index'),
        meta: { title: '用户管理', manage: true },
      },
      {
        path: 'roleManage',
        name: 'roleManage',
        uri: '/systemManage/roleManage',
        component: () => import('@/views/system/role/index'),
        meta: { title: '角色权限', manage: true },
      },
      {
        path: 'sysLog',
        name: 'sysLog',
        uri: '/systemManage/sysLog',
        component: () => import('@/views/system/log/index'),
        meta: { title: '操作日志', manage: true },
      },
    ],
  },
  {
    path: '/scheduleJob',
    name: 'scheduleJob',
    uri: '/scheduleJob',
    component: Layout,
    meta: { title: '任务调度', icon: 'el-icon-timer' },
    children: [
      {
        path: '/',
        name: 'scheduleJob',
        uri: '/scheduleJob',
        component: () => import('@/views/scheduleJob/index'),
        meta: { title: '', manage: true },
      },
    ],
  },
  { path: '/', meta: { hideInMenu: true } },
  { path: '*', redirect: '/404', meta: { hideInMenu: true } },
];

const createRouter = () =>
  new Router({
    routes: constantRoutes,
  });

const router = createRouter();

export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
